from django import template
from django.conf import settings

from sponsorship.models import WebsiteLogo

register = template.Library()

class GetLogosNode(template.Node):

    def __init__(self, var_name):
        self.var_name = var_name

    def render(self, context):
        logos = WebsiteLogo.objects.filter(visible=True)
        context[self.var_name] = logos
        return ''
    
@register.tag
def get_sponsor_logos(parser, token):
    """
    Pull the sponsor logos into the current context

    ::
        {% get_sponsor_logos as var %}

    var:           
        Name of variable to set the feed to in the current context


    use groupby tag on level to get per level breakout
    also highly recomend use of the cache tag.
    """
    args = token.split_contents()
    args.pop(0)
    if len(args) != 2 or args[0] != 'as':
        raise template.TemplateSyntaxError(
            "Malformed arguments to get_sponsor_logos tag.")
    return GetLogosNode(args[-1])

